package com.github.catvod.spider;

import android.text.TextUtils;

import com.github.catvod.bean.Class;
import com.github.catvod.bean.Filter;
import com.github.catvod.bean.Result;
import com.github.catvod.bean.Vod;
import com.github.catvod.crawler.Spider;
import com.github.catvod.net.OkHttp;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

public class M4kav extends Spider {

    private final String url = "https://4k-av.com";

    @Override
    public String homeContent(boolean filter) throws Exception {
        List<Class> classes = new ArrayList<>();
        List<Vod> list = new ArrayList<>();
        Document doc = Jsoup.parse(OkHttp.string(url));
        for (Element a : doc.select("#cate_list").select("li")) {
            String typeName = a.select("a").text();
            String typeId = a.select("a").attr("href");
            classes.add(new Class(typeId, typeName));
        }
        for (Element div : doc.select("#recommlist").select("li")) {
            String id = div.select("a").get(0).attr("href");
            String name = div.select("div.title > a > h2").get(0).text();
            String pic = div.select("img").attr("src");
            String remark = div.select("div.poster").select("span").text();
            Vod vod = new Vod(id, name, pic, remark);;
            list.add(vod);
        }
        return Result.string(classes, list);
    }

    @Override
    public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception {
        List<Vod> list = new ArrayList<>();
        String target = url + tid;
        Document doc = Jsoup.parse(OkHttp.string(target));
        for (Element div : doc.select("div.virow")) {
            String id = div.select("a").get(0).attr("href");
            String name = div.select("a").get(0).select("h2").text();
            String pic = div.select("img").attr("src");
            String remark = div.select("div.resyear").select("label").get(0).text();
            Vod vod = new Vod(id, name, pic, remark);
            list.add(vod);
        }
        return Result.string(list);
    }

    @Override
    public String detailContent(List<String> ids) throws Exception {
        String html = OkHttp.string(url + ids.get(0));
        Document doc = Jsoup.parse(html);
        String name = doc.select("#MainContent_titleh12").select("div").text();
        String pic = doc.select("#videoinfo").select("img").attr("src");
        List<String> vodItems = new ArrayList<>();
        vodItems.add(doc.select("#rtlist").select("span").get(0).text() + "$" + url + ids.get(0));
        Elements sourceList = doc.select("#rtlist").select("a");
        for (Element a : sourceList) {
            String episodeUrl = a.attr("href");
            String episodeName = a.select("span").get(0).text();
            vodItems.add(episodeName + "$" + url + episodeUrl);
        }
        Vod vod = new Vod();
        vod.setTypeName(doc.select("#MainContent_tags").text());
        vod.setVodId(url + ids.get(0));
        vod.setVodDirector("QQ199580494");
        vod.setVodActor("QQ199580494");
        vod.setVodPic(pic);
        vod.setVodName(name.split(" ")[0]);
        vod.setVodPlayFrom("XINLI");
        vod.setVodContent(doc.select("#MainContent_videodesc").text());
        vod.setVodPlayUrl(TextUtils.join("#", vodItems));
        vod.setVodArea(doc.select("#MainContent_titleh12").select("h2").text());
        vod.setVodYear(doc.select("#MainContent_videodetail").select("label").get(3).text());
        vod.setVodRemarks(doc.select("#MainContent_videodetail").select("label").get(1).text());
        return Result.string(vod);
    }

    @Override
    public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
        return Result.get().url(id).parse().string();
    }

    @Override
    public boolean manualVideoCheck() throws Exception {
        return true;
    }

    @Override
    public boolean isVideoFormat(String url) throws Exception {
        return url.contains(".m3u8");
    }

    @Override
    public String searchContent(String key, boolean quick) throws Exception {
        return searchContent("/vod/search/" + key);
    }

    @Override
    public String searchContent(String key, boolean quick, String pg) throws Exception {
        return searchContent("/vod/search/" + key);
    }

    private String searchContent(String query) {
        List<Vod> list = new ArrayList<>();
        Document doc = Jsoup.parse(OkHttp.string(url + query));
        for (Element div : doc.select("div.content-card")) {
            String id = div.select("a").attr("href");
            String name = div.select("div.title").get(0).text();
            String pic = div.select("img").attr("src");
            String tag = div.select(".tag").text();
            String remark = div.select("div.bottom > div").get(0).text();
            Vod vod = new Vod(id, name, pic, tag + (remark.isBlank() ? "" : "/") + remark);
            list.add(vod);
        }
        return Result.string(list);
    }
}

